package com.dushiguang.findhoney.Utils.DeleteData;

import android.content.Context;
import android.os.Environment;

import java.io.File;

/**
 * 按需求清除数据
 * Created by ex-dushiguang201 on 2015-11-26.
 */
public class CleanData {

    /**
     * 清除应用内部缓存
     * @param context
     */
    public void cleanCache(Context context) {
        deleteFilesByDirectory(context.getCacheDir());
    }

    /**
     * 清除应用的数据库
     * @param context
     */
    public void cleanDataBases(Context context) {
        deleteFilesByDirectory(new File("data/data/" + context.getPackageName() + "/databases"));
    }

    /**
     * 清除应用的SharedPreference
     * @param context
     */
    public void cleanSharedPreference(Context context) {
        deleteFilesByDirectory(new File("data/data/" + context.getPackageName() + "/shared_prefs"));
    }

    /**
     * 根据数据库的名称清除数据库
     * @param context
     * @param dbName
     */
    public void cleanDataBaeseByName(Context context, String dbName) {
        context.deleteDatabase(dbName);
    }

    /**
     * 清除//data//data/com.xxx.xxx//files下的文件
     * @param context
     */
    public void cleamFiles(Context context) {
        deleteFilesByDirectory(context.getFilesDir());
    }

    /**
     * 清除外部cache下的内容(/mnt/sdcard/android/data/com.xxx.xxx/cache)
     * @param context
     */
    public void cleanExterCache(Context context) {
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            deleteFilesByDirectory(context.getExternalCacheDir());
        }
    }

    private void deleteFilesByDirectory(File directory) {
        if (directory != null && directory.exists() && directory.isDirectory()) {
            for (File file : directory.listFiles()) {
                file.delete();
            }
        }
    }
}

